home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / CAD_CAM / A7221V1B / PARSE.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  3KB  |  115 lines

  1. /*
  2.    Module:  parse.c
  3.    Date:    3/9/92
  4.    Version: 1.0b
  5.    Author:  Dave Lutz
  6.    Email:   lutz@psych.rochester.edu
  7.    Copyright: 1992 University of Rochester, Psychology Dept.
  8.  
  9.    Disclaimer:  This software is distributed free of charge.  As such, it
  10.                 comes with ABSOLUTELY NO WARRANTY.  The user of the software
  11.                 assumes ALL RISKS associated with its use.
  12.  
  13.                 Your rights to modify and/or distribute this software are
  14.                 outlined in the file ADI7221.DOC.
  15.  
  16.    Purpose: This module provides the function needed to parse the lines from
  17.             an ADI file, breaking the line into a command and its arguments.
  18.  
  19.    Functions Provided:
  20.  
  21.         parse
  22.  
  23.    Functions Required:
  24.  
  25.         none
  26. */
  27.  
  28.  
  29.  
  30. #include <stdlib.h>
  31. #include "adicodes.h"
  32. #include "retcodes.h"
  33.  
  34.  
  35.  
  36. /*
  37.    Function: parse
  38.    Purpose:  Parse a line from an ADI file, converting it to a command and
  39.              its arguments.
  40.  
  41.    Pre: line is a pointer to a line from an ADI file.
  42.         cmd is a pointer to storage for the ADI command.
  43.         arg1 is a pointer to storage for the first argument to the command
  44.         (if it has an argument)
  45.         arg2 is a pointer to storage for the second argument to the command
  46.         (if it has two arguments)
  47.  
  48.    Post: An attempt is made to decode line.
  49.          The ADI command found in line is copied to cmd.
  50.          If the command has an argument it is copied to arg1.
  51.          If the command has two arguments, the second is copied to arg2.
  52.          If an error occurs, NOCMD is copied to cmd.
  53.          If arg1 and/or arg2 are not required for a command, their contents
  54.          are undefined.
  55. */
  56.  
  57. #define BASE 10  /* base to use for string to num conversions */
  58.  
  59. void parse (line, cmd, arg1, arg2)
  60.    char *line;
  61.    int *cmd; 
  62.    unsigned *arg1, *arg2;
  63. {
  64.  
  65.    char *endptr;
  66.    *cmd = *line++ - '0';
  67.  
  68.    switch(*cmd) {
  69.       case END_PLOT:
  70.       case PEN_CHANGE:
  71.       case ABORT_PLOT:
  72.          /* commands with no arg */
  73.          if (*line != (char)NULL)
  74.             *cmd=BADFMT;
  75.          break;
  76.  
  77.       case BEGIN_PLOT:
  78.       case NEW_PEN:
  79.       case SET_SPEED:
  80.       case LINE_TYPE:
  81.          /* commands with 1 arg */
  82.          if (*line++ != ',') {
  83.             *cmd = BADFMT;
  84.             break;
  85.          }
  86.          *arg1 = (unsigned) strtol (line, &endptr, BASE);
  87.          if (*endptr != (char)NULL)
  88.             *cmd = BADFMT;
  89.          break;
  90.  
  91.       case MOVE:
  92.       case DRAW:
  93.          /* commands with 2 args */
  94.          if (*line++ != ',') {
  95.             *cmd = BADFMT;
  96.             break;
  97.          }
  98.          *arg1 = (unsigned) strtol (line, &endptr, BASE);
  99.          if (*endptr != ',') {
  100.             *cmd = BADFMT;
  101.             break;
  102.          }
  103.          line = endptr+1;
  104.          *arg2 = (unsigned) strtol (line, &endptr, BASE);
  105.          if (*endptr != (char)NULL)
  106.             *cmd = BADFMT;
  107.          break;
  108.  
  109.       default:
  110.          *cmd = BADFMT;
  111.          break;
  112.    }
  113. }
  114.  
  115.